package com.msoe.client.panels.eventhandlers;

import org.zenika.widget.client.datePicker.DatePicker;

import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.geocode.DirectionQueryOptions;
import com.google.gwt.maps.client.geocode.DirectionResults;
import com.google.gwt.maps.client.geocode.Directions;
import com.google.gwt.maps.client.geocode.DirectionsCallback;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.msoe.client.MSOERServerAsync;
import com.msoe.client.Server;
import com.msoe.client.panels.AddRide;
import com.msoe.client.panels.MessageBox;
import com.msoe.client.panels.PanelMap;

public class AddRideHandler implements DirectionsCallback,ClickListener {

	private String MSOE_DEFAULT_LOCATION = "1121 North Milwaukee Street Milwaukee WI, 53202";
	private String MSOE_DEFAULT_LNG = "43.045229";
	private String MSOE_DEFAULT_LAT = "-87.907233";
	private AddRide ride;
	private ListBox L_Hour = new ListBox();
	private ListBox L_Min = new ListBox();
	private ListBox L_ampm = new ListBox();
	private ListBox seatsDropDown = new ListBox();
	private DatePicker datePicker;
	private String lastEnteredLng;
	private String lastEnteredLat;

	public AddRideHandler(AddRide ride){
		this.ride = ride;
	}


	public void onClick(Widget sender) {
		if(sender instanceof Button){

			Button callingButton = ((Button) sender);

			if(callingButton.getText().equals("Map Destination")){

				String dest = ride.getDestination();
				Directions.load("from " + MSOE_DEFAULT_LOCATION + " to " +
						dest,new DirectionQueryOptions(ride.getMapWidget()),this);

			}
			else if(callingButton.getText().equals("Add Ride")){

				//get server instance
				MSOERServerAsync server = Server.getInstance().getServer();

				//create call back object
				AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
				{
					public void onSuccess(Boolean result)
					{
						if(result == true) // Register successful
						{
							MessageBox.showMessageDialog("Ride Added Successfully","Your ride has been added successfully.");
						}
						else
						{
							MessageBox.showMessageDialog("Ride Could Not Be Added","Your ride could not be added, please try again later.");
						}
					}

					public void onFailure(Throwable ex)
					{
						Window.alert(ex.getMessage());
					}
				};

				//perform call to server to add ride, default start location is MSOE RWJ dormitory
				server.addRide(ride.getSession().getUser(),MSOE_DEFAULT_LAT,MSOE_DEFAULT_LNG,lastEnteredLat,lastEnteredLng,
						datePicker.getText()+ " " + L_Hour.getItemText(L_Hour.getSelectedIndex()) + ":" +
						L_Min.getItemText(L_Min.getSelectedIndex()) + " " + L_ampm.getItemText(L_ampm.getSelectedIndex()), Integer.parseInt(seatsDropDown.getItemText(seatsDropDown.getSelectedIndex())), callback);

				//close the info window dialog
				ride.getMapWidget().getInfoWindow().close();
			}
			else if(callingButton.getText().equals("Get Ride")){
				PanelMap.showPanel(ride, PanelMap.getPanel("GetRide"));
			}else if(callingButton.getText().equals("Home")){
				PanelMap.showPanel(ride, PanelMap.getPanel("UserHome"));
			}
		}
	}


	public void onFailure(int statusCode) {

		MessageBox.showMessageDialog("Invalid Destination Address"
				,"The destination address you entered could not be found or is ambigious.");

	}

	public void onSuccess(DirectionResults result) {

		//get last marker as destination
		Marker dest = result.getMarkers().get(result.getMarkers().size() - 1);

		lastEnteredLng = String.valueOf(dest.getLatLng().getLongitude());
		lastEnteredLat = String.valueOf(dest.getLatLng().getLatitude());
		
		//center on destination
		ride.getMapWidget().setCenter(dest.getLatLng());
		
		//open the InfoWindow above the destination marker dest
		ride.getMapWidget().getInfoWindow().open(dest.getLatLng(),getNewRideInfoWindowContent());

		ride.getMapWidget().getInfoWindow().setVisible(true);

	}


	private void fillBoxs() {
		for(int i = 1;i<13;i++){
			L_Hour.addItem(i+"");
		}
		L_Min.addItem("00");
		L_Min.addItem("15");
		L_Min.addItem("30");
		L_Min.addItem("45");
		
		L_ampm.addItem("pm");
		L_ampm.addItem("am");

	}

	private InfoWindowContent getNewRideInfoWindowContent(){

		HorizontalPanel headerPanel = new HorizontalPanel();
		HorizontalPanel timePanel = new HorizontalPanel();
		HorizontalPanel seatsPanel = new HorizontalPanel();
		HorizontalPanel submitPanel = new HorizontalPanel();

		VerticalPanel mainPanel = new VerticalPanel();

		Label header = new Label("Please Provide the Following Information");
		header.addStyleDependentName("MapInfoWindow");
		headerPanel.add(header);

		Label leavingTime = new Label("Latest Time Leaving: ");
		leavingTime.addStyleDependentName("MapInfoWindow");
		datePicker = new DatePicker();

		timePanel.add(leavingTime);
		fillBoxs();
		timePanel.add(L_Hour);
		Label colon = new Label(":");
		colon.addStyleDependentName("MapInfoWindow");
		timePanel.add(colon);
		timePanel.add(L_Min);
		timePanel.add(L_ampm);
		timePanel.add(datePicker);
		Label seats = new Label("Seats:");
		seats.addStyleDependentName("MapInfoWindow");
		seatsPanel.add(seats);

		seatsDropDown = new ListBox();

		int seatsMax = 10;
		for(int i = 1;i < seatsMax; i++){
			seatsDropDown.addItem(String.valueOf(i),String.valueOf(i));
		}

		seatsPanel.add(seatsDropDown);

		Button submitButton = new Button("Add Ride");
		submitButton.addClickListener(this);
		submitPanel.add(submitButton);
		submitPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		mainPanel.add(headerPanel);
		mainPanel.add(timePanel);
		mainPanel.add(seatsPanel);
		mainPanel.add(submitPanel);
		return new InfoWindowContent(mainPanel);

	}


}
